package com.ohos.carbon.slice;

import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

public class CharView extends Component implements Component.DrawTask, Component.TouchEventListener {
    public int xPoint = 80;    //原点的X坐标
    public int yPoint = Constant.point.getPointYToInt() / 2;     //原点的Y坐标
    public int xScale = 55;     //X的刻度长度
    public int yScale = 40;     //Y的刻度长度
    public int xLength = Constant.point.getPointXToInt() - 100;        //X轴的长度
    public int yLength = Constant.point.getPointYToInt() / 2 - 100;        //Y轴的长度
    public String[] xLabel;    //X的刻度
    public String[] yLabel;    //Y的刻度
    public String[] Data;      //数据
    public String Title;    //显示的标题

    public CharView(Context context) {
        super(context);
        addDrawTask(this::onDraw);
    }

    public void setInfo(String[] xLabels, String[] yLabels, String[] AllData, String strTitle) {
        xLabel = xLabels;
        yLabel = yLabels;
        Data = AllData;
        Title = strTitle;
        xScale = xLength / AllData.length;//实际X的刻度长度
        yScale = yLength / yLabels.length;
    }


    @Override
    public void onDraw(Component component, Canvas canvas) {
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL_STYLE);
        paint.setAntiAlias(true);//去锯齿
        paint.setColor(Color.BLACK);//颜色
        Paint paint1 = new Paint();
        paint1.setStyle(Paint.Style.FILL_STYLE);
        paint1.setAntiAlias(true);//去锯齿
        paint1.setColor(Color.DKGRAY);
        paint.setTextSize(36);  //设置轴文字大小
        Point startPoint = new Point(xPoint, yPoint - yLength);
        Point endPoint = new Point(xPoint, yPoint);
        canvas.drawLine(startPoint, endPoint, paint);   //轴线
        for (int i = 0; i * yScale < yLength - yScale; i++) {
            Point kStartPoint = new Point(xPoint, yPoint - i * yScale);
            Point kEndPoint = new Point(xPoint + 5, yPoint - i * yScale);
            canvas.drawLine(kStartPoint, kEndPoint, paint);  //刻度
            canvas.drawText(paint, yLabel[i], xPoint - 30, yPoint - i * yScale + 5);  //文字

            Point arrawStartPoint = new Point(xPoint, yPoint - yLength);
            Point arrawEndPoint = new Point(xPoint - 3, yPoint - yLength + 6);
            canvas.drawLine(arrawStartPoint, arrawEndPoint, paint);  //箭头

            Point arrawYStartPoint = new Point(xPoint, yPoint - yLength);
            Point arrawYEndPoint = new Point(xPoint + 3, yPoint - yLength + 6);
            canvas.drawLine(arrawYStartPoint, arrawYEndPoint, paint);

            //设置字体的大小角度等
            paint.setTextSize(36);
            drawText(canvas, "        单位:kWh", xPoint - 5, yPoint - yLength + yScale - 5, paint, -90);

            //设置X轴
            paint.setTextSize(48);
            Point arrawXStartPoint = new Point(xPoint, yPoint);
            Point arrawXEndPoint = new Point(xPoint + xLength, yPoint);
            canvas.drawLine(arrawXStartPoint, arrawXEndPoint, paint);   //轴线
            for (int j = 0; j * xScale < xLength - xScale; j++) {

                Point arrawXKStartPoint = new Point(xPoint + i * xScale, yPoint);
                Point arrawXKEndPoint = new Point(xPoint + i * xScale, yPoint - 5);
                canvas.drawLine(arrawXKStartPoint, arrawXKEndPoint, paint);  //刻度

                drawText(canvas, xLabel[i], xPoint + i * xScale,
                        yPoint + 40, paint, -45); // 文字

                // 数据值
                if (j > 0 && YCoord(Data[j - 1]) != -999
                        && YCoord(Data[j]) != -999) // 保证有效数据
                {
                    Point arrawXSStartPoint = new Point(xPoint + (j - 1) * xScale,
                            YCoord(Data[j - 1]));
                    Point arrawXSEndPoint = new Point(xPoint + j * xScale,
                            YCoord(Data[j]));
                    canvas.drawLine(arrawXSStartPoint, arrawXSEndPoint, paint);

                    canvas.drawCircle(xPoint + i * xScale, YCoord(Data[i]), 2,
                            paint);
                }
            }
            Point arrawXJStartPoint = new Point(xPoint + xLength, yPoint);
            Point arrawXJEndPoint = new Point(xPoint + xLength - 6, yPoint - 3);
            canvas.drawLine(arrawXJStartPoint, arrawXJEndPoint, paint);    //箭头
            Point arrawYJStartPoint = new Point(xPoint + xLength, yPoint);
            Point arrawYJEndPoint = new Point(xPoint + xLength - 6, yPoint + 3);
            canvas.drawLine(arrawYJStartPoint, arrawYJEndPoint, paint);

            //设置标题位置
            paint.setTextSize(48);
            canvas.drawText(paint, Title, xLength / 2 - 28, 50);

        }

    }


    //设置文字显示方向
    void drawText(Canvas canvas, String text, float x, float y, Paint paint, float angle) {
        if (angle != 0) {
            canvas.rotate(angle, x, y);
        }
        canvas.drawText(paint, text, x, y);
        if (angle != 0) {
            canvas.rotate(-angle, x, y);
        }
    }

    private int YCoord(String y0)  //计算绘制时的Y坐标，无数据时返回-999
    {
        int y;
        try {
            y = Integer.parseInt(y0);
        } catch (Exception e) {
            return -999;    //出错则返回-999
        }
        try {
            return yPoint - y * yScale / Integer.parseInt(yLabel[1]);
        } catch (Exception e) {
        }
        return y;
    }


    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        return false;
    }
}
